package per.ethan.cloud.common.service;

import per.ethan.cloud.common.domain.Page;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

/**
 * @author Ethan
 * @date 2020/03/10 19:57
 */
public interface BaseService<Entity> {

    Page<Entity> findByPage(Page<Entity> page);

    Long save(Entity entity);

    boolean update(Entity entity);

    List<Long> batchSave(List<Entity> entities);

    boolean batchUpdate(Entity entity, List<String> idList);

    boolean deleteById(Long id);

    boolean deleteByWhere(Map<String, Object> map);

    boolean logicDeleteByWhere(Map<String, Object> map);

    boolean logicDeleteById(Long id);

    boolean batchDelete(List<Long> idList);

    boolean logicBatchDelete(List<Long> idList);

    Entity findById(Long id);

    Entity findByName(String name);

    List<Entity> findAll();

    List<Entity> findByMap(Map<String, Object> map);

    default Class<?> getTarget() {

        Class clazz = this.getClass();
        //获取父类
        Type type = clazz.getGenericSuperclass();
        //获取泛型
        ParameterizedType params = (ParameterizedType) type;
        //获取泛型的类型
        Type[] actualTypeArguments = params.getActualTypeArguments();
        return (Class) actualTypeArguments[0];

    }

}
